package biolabs.query;

import java.math.BigDecimal;

import biolabs.core.BioObject;

/**
 * Predicate that compares the results of two expressions for inequality.
 * If either of the expressions evaluates to {@code null}, the predicate
 * evaluates to {@code false}. If both expresssions evaluate to numeric
 * values, the numeric values are compared. Otherwise, the values are
 * compared by the {@code equals(Object)} method of the first one.
 *
 * @author Tomas Mikula
 */
public class NotEqual implements Predicate {

	private final Expression a;
	private final Expression b;

	public NotEqual(Expression a, Expression b) {
		if(a == null || b == null)
			throw new NullPointerException();
		this.a = a;
		this.b = b;
	}

	@Override
	public boolean eval(BioObject obj) {
		Object valA = a.eval(obj);
		Object valB = b.eval(obj);

		if(valA == null || valB == null)
			return false;

		BigDecimal bigA = NumericPredicate.toBigDecimal(valA);
		BigDecimal bigB = NumericPredicate.toBigDecimal(valB);
		if(bigA != null && bigB != null)
			return bigA.compareTo(bigB) != 0;

		return !valA.equals(valB);
	}

}
